package com.cykj.pos.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysDictData;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.common.utils.StringUtils;
import com.cykj.pos.domain.*;
import com.cykj.pos.domain.dto.BillQueryDTO;
import com.cykj.pos.domain.dto.IntegralDTO;
import com.cykj.pos.domain.dto.IntegralDetailDTO;
import com.cykj.pos.domain.dto.OrderDTO;
import com.cykj.pos.enums.bizstatus.BizStatusContantEnum;
import com.cykj.pos.profit.dto.AddressDTO;
import com.cykj.pos.profit.dto.BizMerchAddressDTO;
import com.cykj.pos.profit.dto.TeamPartnerDTO;
import com.cykj.pos.profit.dto.WalletDTO;
import com.cykj.pos.service.*;
import com.cykj.pos.util.DESHelperUtil;
import com.cykj.pos.util.LoginUserUtils;
import com.cykj.system.service.ISysDictDataService;
import com.cykj.system.service.ISysUserService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.aspectj.runtime.internal.Conversions.longValue;

/**
 *积分管理Controller
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/api/v2/integral")
public class PosV2AppIntegralController {
    private final IBizWalletService walletService;
    private final IBizMerchIntegralService integralService;
    private final IBizMerchantService merchantService;
    private final IBizMerchOrderService merchOrderService;
    private final IBizMerchAddressService merchAddressService;
    private final IBizPosTypeService iBizPosTypeService;

    /**
     * 我的积分首页  获得积分  传递userId  用户id
     * @param integralDTO
     * @return
     */
    @PostMapping("/homepage")
    public AjaxResult integralHomePage(@RequestBody IntegralDTO integralDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的积分首页数据获取成功");
        Long userId = LoginUserUtils.getLoginUserId();
        BizWallet wallet = walletService.getMyWalletByUserId(integralDTO.getUserId());
        IntegralDTO integral = new IntegralDTO();
        integral.setIntegral(wallet.getIntegral()); // 设置通用积分
        integral.setActivityIntegral(wallet.getActivityIntegral());// 设置活动积分
        integral.setSecretKey(wallet.getSecretKey()); // 秘钥
        ajaxResult.put("data",integral);
        return ajaxResult;
    }

    /**
     * 获得积分列表  传入 用户id  积分类型（transType 不填入就是全部 收入1 支出 2）
     * @param integralDTO
     * @return
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody IntegralDTO integralDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的积分明细数据列表获取成功");
        List<Map<String, IntegralDetailDTO>> integralList = integralService.getIntegralList(integralDTO);
        ajaxResult.put("data",integralList);
        return ajaxResult;
    }

    /**
     * 获得积分类型
     * @return
     */
    @PostMapping("/get/posType")
    public AjaxResult getPosType(){
        AjaxResult ajaxResult = AjaxResult.success("获得机具类型");
        List<BizPosType> list = integralService.getPosTypeList();
        ajaxResult.put("data",list);
        return ajaxResult;
    }

    /**
     * 获得Post机器品牌和类型
     * @return
     */
    @PostMapping("/get/posBrandType")
    public AjaxResult getPosBrandAndType(){
        AjaxResult ajaxResult = AjaxResult.success("获得机具类型");
        List<BizPosBrandType> list = integralService.getPosBrandList();
        ajaxResult.put("data",list);
        return ajaxResult;
    }

    /**
     * 获得Post机器品牌和类型(终端查询)
     * @return
     */
    @PostMapping("/get/posBrandTypeAll")
    public AjaxResult getPosBrandAndTypeAll(){
        AjaxResult ajaxResult = AjaxResult.success("获得机具类型");
        List<BizPosBrandType> list = integralService.getPosBrandList();
        BizPosBrandType bizPosBrandType = new BizPosBrandType();
        List<BizPosType> list1 = iBizPosTypeService.list();
        bizPosBrandType.setId(3L);
        bizPosBrandType.setBrandName("全部");
        bizPosBrandType.setPosTypeList(list1);
        list.add(bizPosBrandType);
        Collections.reverse(list);
        ajaxResult.put("data",list);
        return ajaxResult;
    }

    /**
     * 积分兑换 选择兑换对象
     * @param integralDTO
     * @return
     */
    @PostMapping("/convert")
    public AjaxResult getParentMerchByMerchId(@RequestBody IntegralDTO integralDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的选择兑换数据获取成功");
        List<BizMerchant> parentMerchList = merchantService.getParentMerchByUserId(integralDTO.getUserId());
        ajaxResult.put("data",parentMerchList);
        return ajaxResult;
    }

    /**
     * 积分兑换 选择地址
     * @return
     */
    @PostMapping("/orderAddress")
    public AjaxResult getOrderAddress(){
        AjaxResult ajaxResult = AjaxResult.success("我的地址获取成功");
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchAddressDTO list = merchAddressService.queryAddressListByOrder(userId);
        ajaxResult.put("data",list);
        return ajaxResult;
    }

    @PostMapping("/order")
    public AjaxResult submitOrder(@RequestBody OrderDTO orderDTO){
        AjaxResult ajaxResult = AjaxResult.success("我的订单提交成功");
        Long userId =  orderDTO.getUserId();
        BizMerchant merchant =  merchantService.getMerchantByUserId(userId);
        orderDTO.setMerchId(merchant.getMerchId());
        BizWallet wallet= walletService.getMyWalletByUserId(merchant.getUserId());
        String integral = DESHelperUtil.decrypt(wallet.getSecretKey(),wallet.getIntegral());
        long integeralDemo = Long.parseLong(integral);
        if (orderDTO.getMoney()<=integeralDemo){
            BizMerchOrder order = merchOrderService.submitOrder(orderDTO);
            OrderDTO orderDTO1 = new OrderDTO();
            orderDTO1.setOrderId(order.getOrderId());
            AddressDTO address = merchAddressService.getOrderAddress(orderDTO1);
            ajaxResult.put("data",order);
            ajaxResult.put("dataAddress",address);
        }else {
            List<OrderDTO> list=new ArrayList<>();
            AjaxResult Result = AjaxResult.error("您的积分不足");
            Result.put("data",list);
            return Result;
        }
        return ajaxResult;
    }
    /**
     * 按名字电话查询  备用
     */
   /* @PostMapping("/teamList")
    public AjaxResult getTeamList(@RequestBody TeamPartnerDTO DTO) {
        AjaxResult ajaxResult = AjaxResult.success("数据获取成功");
        if(DTO.getMessage()==null || DTO.getMessage()==""){
            List<TeamPartnerDTO> list1=new ArrayList<>();
            ajaxResult.put("data",list1);
            return ajaxResult;
        }
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        Long pId= merchant.getParentId();
        TeamPartnerDTO teamPartnerDTO=integralService.getLastPerson(pId);
        if(teamPartnerDTO !=null){
        TeamPartnerDTO dto=new TeamPartnerDTO();
        if(teamPartnerDTO.getParentId()!=0){
            dto.setParentId(teamPartnerDTO.getParentId());
            dto.setMerchId(teamPartnerDTO.getMerchId());
        }else if(teamPartnerDTO.getParentId()==0){
            dto.setParentId(pId);
            dto.setMerchId(merchant.getMerchId());
        }
            for(int i=0;i>=0;i++){
                BizMerchant upmerchant=merchantService.getMerchantByMerchId(teamPartnerDTO.getParentId());
              if( upmerchant !=null && upmerchant.getParentId()!=0){
                  dto.setParentId(upmerchant.getParentId());
                  dto.setMerchId(upmerchant.getMerchId());
                  teamPartnerDTO.setParentId(upmerchant.getParentId());
              }else if(upmerchant ==null ||upmerchant.getParentId()==0){
                  break;
              }
        }
            DTO.setParentId(dto.getParentId());
            DTO.setUserId(userId);
            DTO.setMerchId(dto.getMerchId());
            if(StringUtils.isNumeric(DTO.getMessage())){
                DTO.setMobile(DTO.getMessage());
            }else{
                DTO.setMerchName(DTO.getMessage());
            }
        }
        List<TeamPartnerDTO> list=integralService.getTeamParner(DTO);
        ajaxResult.put("data", list);
        return ajaxResult;
    }*/
    /**
     * 按名字电话查询
     */
    @PostMapping("/teamList")
    public AjaxResult getTeamList(@RequestBody TeamPartnerDTO DTO){
        AjaxResult ajaxResult = AjaxResult.success("数据获取成功");
        if(DTO.getMessage()==null || DTO.getMessage()==""){
            List<TeamPartnerDTO> list1=new ArrayList<>();
            ajaxResult.put("data",list1);
            return ajaxResult;
        }
        Long userId = LoginUserUtils.getLoginUserId();
        DTO.setUserId(userId);
        if(StringUtils.isNumeric(DTO.getMessage())){
            DTO.setMobile(DTO.getMessage());
        }else{
            DTO.setMerchName(DTO.getMessage());
        }
        List<TeamPartnerDTO> list=integralService.getAllParner(DTO);
        ajaxResult.put("data", list);
        return ajaxResult;
    }
}
